package com.gem.controller;

import com.gem.domain.Company;
import com.gem.domain.CompanyContact;
import com.gem.domain.User;
import com.gem.service.CompanyContactService;
import com.gem.service.CompanyService;
import com.gem.service.UserService;
import com.gem.util.MessageUtil;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * @Author: 玲
 * @Description:
 * @create 2019-10-24 21:32
 * @Modified By:
 */
@Controller
public class CompanyContactController {

    @Autowired
    private CompanyContactService service;
    @Autowired
    private CompanyService companyService;
    @Autowired
    private UserService userService;

    @RequestMapping("/contact/findAll")
    public String findAll(Integer coContactCompanyId, Model model,HttpSession session,
                          @RequestParam(value = "currentPage", defaultValue = "1") int currentPage,
                          @RequestParam(value = "pageSize", defaultValue = "5", required = false) int pageSize) {
        model.addAttribute("currentPage", currentPage);
        model.addAttribute("pageSize", pageSize);
        User user = (User)session.getAttribute("user");
        try {
            List<CompanyContact> contactList = null;
            if (user.getAuthority() != 3){
                contactList  = service.findAll(coContactCompanyId);
            }else {
                contactList = service.findAllByUid(coContactCompanyId,user.getUid());
            }

            if (contactList != null && contactList.size() != 0) {
                model.addAttribute("coContactCompanyId", coContactCompanyId);
                model.addAttribute("contactList", contactList);
            } else {
                model.addAttribute("coContactCompanyId", coContactCompanyId);
                model.addAttribute("info", "没有数据请先添加！");
            }

        } catch (Exception e) {
            e.printStackTrace();
            MessageUtil.returnInfoPage("3", "/contact/findAll?coContactCompanyId=" + coContactCompanyId, "服务器错误！请联系管理员", model);
            return "message";
        }
        return "company/customer_contact";
    }

    @RequestMapping("/contact/saveUI")
    public String saveUI(Integer coContactCompanyId, Model model) {
        try {
            Company company = companyService.findById(coContactCompanyId);
            model.addAttribute("company", company);
        } catch (Exception e) {
            e.printStackTrace();
            MessageUtil.returnInfoPage("3", "/contact/findAll?coContactCompanyId=" + coContactCompanyId, "服务器错误！请联系管理员", model);
            return "message";
        }
        return "company/customer_contact_insert";
    }


    @RequestMapping("/contact/save")
    public String save(CompanyContact companyContact, Model model, HttpSession session) {
        try {
            User user = (User) session.getAttribute("user");
            companyContact.setCoContactWriterId(user.getUid());
            service.save(companyContact);
            MessageUtil.returnInfoPage("3", "/contact/findAll?coContactCompanyId=" + companyContact.getCoContactCompanyId(), "添加成功!", model);
        } catch (Exception e) {
            e.printStackTrace();
            MessageUtil.returnInfoPage("3", "/contact/findAll?coContactCompanyId=" + companyContact.getCoContactCompanyId(), "服务器错误！请联系管理员", model);
        }
        return "message";

    }

    @RequestMapping("/contact/updateUI")
    public String updateUI(Integer coContactId, Integer coContactCompanyId, Model model, String cp) {
        try {
            CompanyContact companyContact = service.findById(coContactId);
            Company company = companyService.findById(coContactCompanyId);
            model.addAttribute("contact", companyContact);
            model.addAttribute("company", company);
            model.addAttribute("cp", cp);
        } catch (Exception e) {
            e.printStackTrace();
            MessageUtil.returnInfoPage("3", "/contact/findAll?coContactCompanyId=" + coContactCompanyId, "服务器错误！请联系管理员", model);
        }
        return "company/customer_contact_update";
    }

    @RequestMapping("/contact/update")
    public String update(CompanyContact companyContact, Model model) {
        try {
            if (service.update(companyContact) == 1) {
                MessageUtil.returnInfoPage("3", "/contact/findAll?coContactCompanyId=" + companyContact.getCoContactCompanyId(), "修改成功!", model);
            } else {
                MessageUtil.returnInfoPage("3", "/contact/findAll?coContactCompanyId=" + companyContact.getCoContactCompanyId(), "未作修改!", model);
            }
        } catch (Exception e) {
            e.printStackTrace();
            MessageUtil.returnInfoPage("3", "/contact/findAll?coContactCompanyId=" + companyContact.getCoContactCompanyId(), "服务器错误！请联系管理员", model);
        }
        return "message";

    }

    @RequestMapping("/contact/deleteById")
    public String deleteById(Integer coContactId, Integer coContactCompanyId, Model model) {
        try {
            service.deleteById(coContactId);
            MessageUtil.returnInfoPage("3", "/contact/findAll?coContactCompanyId=" + coContactCompanyId, "删除成功!", model);
        } catch (Exception e) {
            e.printStackTrace();
            MessageUtil.returnInfoPage("3", "/contact/findAll?coContactCompanyId=" + coContactCompanyId, "服务器错误！请联系管理员", model);
        }
        return "message";

    }

    @RequestMapping("/contact/deleteAll")
    public String deleteAll(int[] coContactIds, Integer coContactCompanyId, Model model) {
        try {
            service.deleteAll(coContactIds);
            MessageUtil.returnInfoPage("3", "/contact/findAll?coContactCompanyId=" + coContactCompanyId, "删除成功!", model);
        } catch (Exception e) {
            e.printStackTrace();
            MessageUtil.returnInfoPage("3", "/contact/findAll?coContactCompanyId=" + coContactCompanyId, "服务器错误！请联系管理员", model);
        }
        return "message";

    }

}
